package com.srd.phone.application;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.PreferenceUtils;

import java.util.HashMap;
import java.util.Map;

public class HttpStringRequest extends StringRequest {


    public HttpStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        setRetryPolicy(new DefaultRetryPolicy(500 * 1000, 0, 1f));
    }

    public HttpStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
        setRetryPolicy(new DefaultRetryPolicy(500 * 1000, 0, 1f));
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("Cookie", PreferenceUtils.getInstance().getData(Constants.PREFERENCE_COOKIE,""));
//        map.put("authorization", UserUtils.getUserInfo().token);
        map.put("authorization", "cc562161dc5de376332dad11ad509335");
        return map;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return super.getParams();
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String cookie = response.headers.get("Set-Cookie");
        if (!TextUtils.isEmpty(cookie)) {
            PreferenceUtils.getInstance().putData(Constants.PREFERENCE_COOKIE, cookie);
        }
        return super.parseNetworkResponse(response);
    }
}
